package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dlwx.parttimetalent.AlPay.AliPayUtiles;
import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.AliPayResBean;
import com.dlwx.parttimetalent.bean.AuthCoreBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.DialogUtils;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CertificateActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_phone)
    RelativeLayout llPhone;
    @BindView(R.id.tv_identity)
    TextView tvIdentity;
    @BindView(R.id.ll_identity)
    RelativeLayout llIdentity;
    @BindView(R.id.tv_alipay)
    TextView tvAlipay;
    @BindView(R.id.ll_alipay)
    RelativeLayout llAlipay;
    @BindView(R.id.tv_weixin)
    TextView tvWeixin;
    @BindView(R.id.ll_wixin)
    RelativeLayout llWixin;
    @BindView(R.id.tv_weibo)
    TextView tvWeibo;
    @BindView(R.id.ll_weibo)
    RelativeLayout llWeibo;
    @BindView(R.id.tv_zhima)
    TextView tvZhima;
    @BindView(R.id.ll_zhima)
    RelativeLayout llZhima;
    @BindView(R.id.tv_skill)
    TextView tvSkill;
    @BindView(R.id.ll_skill)
    RelativeLayout llSkill;
    @BindView(R.id.tv_education)
    TextView tvEducation;
    @BindView(R.id.ll_education)
    RelativeLayout llEducation;
    @BindView(R.id.tv_driver)
    TextView tvDriver;
    @BindView(R.id.ll_driver)
    RelativeLayout llDriver;
    @BindView(R.id.image_weibo)
    ImageView imageWeibo;
    @BindView(R.id.image_iden)
    ImageView imageIden;
    @BindView(R.id.image_alipay)
    ImageView imageAlipay;
    @BindView(R.id.image_weixin)
    ImageView imageWeixin;
    @BindView(R.id.image_zhima)
    ImageView imageZhima;
    @BindView(R.id.image_skill)
    ImageView imageSkill;
    @BindView(R.id.image_edu)
    ImageView imageEdu;
    @BindView(R.id.image_driver)
    ImageView imageDriver;
    @BindView(R.id.image_phone)
    ImageView imagePhone;
    private AuthCoreBean.BodyBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_certificate);
        ButterKnife.bind(this);
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getDatas();
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.MyAuthCore, this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        AuthCoreBean parentBean = DealResult.getInstance().dealDatas(CertificateActivity.this, response, new TypeToken<AuthCoreBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            setDatas(parentBean.getBody());
                        } else {
                            ToastUtils.toastForShort(CertificateActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(CertificateActivity.this,getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(AuthCoreBean.BodyBean body) {
        bean=body;
        AuthCoreBean.BodyBean.PhoneAuthBean bean1 = body.getPhoneAuth();
        tvPhone.setText(bean1.getText());
        if (bean1.getStatus() == 1) {
//            tvPhone.setText("已认证");
            tvPhone.setTextColor(ContextCompat.getColor(this, R.color.second_textcolor));
            imagePhone.setVisibility(View.INVISIBLE);
        }else if(bean1.getStatus()==3){
//            tvPhone.setText("审核中");
        }else if(bean1.getStatus()==2){
//            tvPhone.setText("审核失败");
        }
        AuthCoreBean.BodyBean.IdentAuthBean bean2 = body.getIdentAuth();
        tvIdentity.setText(bean2.getText());
        if (bean2.getStatus() == 1) {
//            tvIdentity.setText("已认证");
            tvIdentity.setTextColor(ContextCompat.getColor(this, R.color.second_textcolor));
            imageIden.setVisibility(View.INVISIBLE);
        }else if(bean2.getStatus()==3){
//            tvIdentity.setText("审核中");
        }else if(bean2.getStatus()==2){
//            tvIdentity.setText("审核失败");
        }
        AuthCoreBean.BodyBean.AlipayAuthBean bean3 = body.getAlipayAuth();
        tvAlipay.setText(bean3.getText());
        if (bean3.getStatus() == 1) {
//            tvAlipay.setText("已认证");
            tvAlipay.setTextColor(ContextCompat.getColor(this, R.color.second_textcolor));
            imageAlipay.setVisibility(View.INVISIBLE);
        }else if(bean3.getStatus()==3){
//            tvAlipay.setText("审核中");
        }else if(bean3.getStatus()==2){
//            tvAlipay.setText("审核失败");
        }
        AuthCoreBean.BodyBean.WxAuthBean bean4 = body.getWxAuth();
        tvWeixin.setText(bean4.getText());
        if (bean4.getStatus() == 1) {
//            tvWeixin.setText("已认证");
            tvWeixin.setTextColor(ContextCompat.getColor(this, R.color.second_textcolor));
            imageWeixin.setVisibility(View.INVISIBLE);
        }else if(bean4.getStatus()==3){
//            tvWeixin.setText("审核中");
        }else if(bean4.getStatus()==2){
//            tvWeixin.setText("审核失败");
        }
        AuthCoreBean.BodyBean.SinaAuthBean bean5 = body.getSinaAuth();
        tvWeibo.setText(bean5.getText());
        if (bean5.getStatus() == 1) {
//            tvWeibo.setText("已认证");
            tvWeibo.setTextColor(ContextCompat.getColor(this, R.color.second_textcolor));
            imageWeibo.setVisibility(View.INVISIBLE);
        } else if(bean5.getStatus()==3){
//            tvWeibo.setText("审核中");
        }else if(bean5.getStatus()==2){
//            tvWeibo.setText("审核失败");
        }
        AuthCoreBean.BodyBean.SkillAuthBean bean6 = body.getSkillAuth();
        tvSkill.setText(bean6.getText());
        if (bean6.getStatus() == 1) {
//            tvSkill.setText("已认证");
            tvSkill.setTextColor(ContextCompat.getColor(this, R.color.second_textcolor));
            imageSkill.setVisibility(View.INVISIBLE);
        } else if(bean6.getStatus()==3){
//            tvSkill.setText("审核中");
        }else if(bean6.getStatus()==2){
//            tvSkill.setText("审核失败");
        }
        AuthCoreBean.BodyBean.EduAuthBean bean7 = body.getEduAuth();
        tvEducation.setText(bean7.getText());
        if (bean7.getStatus() == 1) {
//            tvEducation.setText("已认证");
            tvEducation.setTextColor(ContextCompat.getColor(this, R.color.second_textcolor));
            imageEdu.setVisibility(View.INVISIBLE);
        } else if(bean7.getStatus()==3){
//            tvEducation.setText("审核中");
        }else if(bean7.getStatus()==2){
//            tvEducation.setText("审核失败");
        }
        AuthCoreBean.BodyBean.DriveAuthBean bean8 = body.getDriveAuth();
        tvDriver.setText(bean8.getText());
        if (bean8.getStatus() == 1) {
//            tvDriver.setText("已认证");
            tvDriver.setTextColor(ContextCompat.getColor(this, R.color.second_textcolor));
            imageDriver.setVisibility(View.INVISIBLE);
        } else if(bean8.getStatus()==3){
//            tvDriver.setText("审核中");
        }else if(bean8.getStatus()==2){
//            tvDriver.setText("审核失败");
        }
    }


    private void initView() {
        tvMiddleTitle.setText("认证中心");
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.title_left_one_btn, R.id.ll_phone, R.id.ll_identity, R.id.ll_alipay
            , R.id.ll_wixin, R.id.ll_weibo, R.id.ll_zhima, R.id.ll_skill, R.id.ll_education
            , R.id.ll_driver})
    public void onViewClicked(View view) {
        Intent i=new Intent(this,CertificateResActivity.class);
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.ll_phone:
//                startActivity(new Intent(this, PhoneActivity.class));
                break;
            case R.id.ll_identity:
                int status1 = bean.getIdentAuth().getStatus();
                if(status1==Constant.STATUS_NEVER){
                    startActivity(new Intent(this, IdentityActivity.class));
                }else{
                    i.putExtra("flag",Constant.ZERO);
                    i.putExtra("type",status1);
                    i.putExtra("reason",bean.getIdentAuth().getReason());
                    startActivity(i);
                }
                break;
            case R.id.ll_alipay:
                if(TextUtils.equals(tvAlipay.getText().toString(),"未认证")){
                    final NormalDialog dialog = DialogUtils.getAliPayDialog(this, "", "使用您要认证的支付宝充值0.01元，则完成认证");
                    dialog.setOnBtnClickL(new OnBtnClickL() {
                                              @Override
                                              public void onBtnClick() {
                                                  dialog.dismiss();

                                              }
                                          },
                            new OnBtnClickL() {
                                @Override
                                public void onBtnClick() {
                                    dialog.dismiss();
                                    confirm();
                                }
                            });
                }
                break;
            case R.id.ll_wixin:
                ToastUtils.toastForShort(this,"微信登录，并绑定当前手机号，即可认证成功");
                break;
            case R.id.ll_weibo:
                ToastUtils.toastForShort(this,"微博登录，并绑定当前手机号，即可认证成功");
                break;
            case R.id.ll_zhima:
                break;
            case R.id.ll_skill:
                int status2 = bean.getSkillAuth().getStatus();
                if(status2==Constant.STATUS_NEVER){
                    startActivity(new Intent(this, SkillCertifyActivity.class));
                }else{
                    i.putExtra("flag",Constant.ONE);
                    i.putExtra("type",status2);
                    i.putExtra("reason",bean.getSkillAuth().getReason());
                    startActivity(i);
                }
                break;
            case R.id.ll_education:
                int status3 = bean.getEduAuth().getStatus();
                if(status3==Constant.STATUS_NEVER){
                    startActivity(new Intent(this, EducateActivity.class));
                }else{
                    i.putExtra("flag",Constant.TWO);
                    i.putExtra("type",status3);
                    i.putExtra("reason",bean.getEduAuth().getReason());
                    startActivity(i);
                }
                break;
            case R.id.ll_driver:
                int status4 = bean.getDriveAuth().getStatus();
                if(status4==Constant.STATUS_NEVER){
                    startActivity(new Intent(this, DriverActivity.class));
                }else{
                    i.putExtra("flag",Constant.THREE);
                    i.putExtra("reason",bean.getDriveAuth().getReason());
                    i.putExtra("type",status4);
                    startActivity(i);
                }
                break;
        }
    }

    private void confirm( ) {
        loadingDialog.show();
        OkgoUtil.post(Http.SubAuthData, this)
                .params("authType",3)
                .params("title1","")
                .params("img1","")
                .params("img2","")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        AliPayResBean parentBean = DealResult.getInstance().dealDatas(CertificateActivity.this
                                , response, new TypeToken<AliPayResBean>() {
                                }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            AliPayUtiles utiles = new AliPayUtiles(CertificateActivity.this, parentBean.getBody());
                            utiles.setOnResultListener(new AliPayUtiles.PayResultOnListener() {
                                @Override
                                public void getResult(Boolean result) {
                                    if (result) {
                                        ToastUtils.toastForShort(CertificateActivity.this,"支付成功");
                                        getDatas();
                                    }
                                }
                            });
                            utiles.startPay();
                        } else {
                        }
                        ToastUtils.toastForShort(CertificateActivity.this, parentBean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(CertificateActivity.this,getString(R.string.bad_net));
                    }
                });
    }
}
